package com.hlingsoft.cms.action.manage;

import com.hlingsoft.cms.constant.OrderInfoConstant;
import com.hlingsoft.cms.dao.GoodsDao;
import com.hlingsoft.cms.dao.ImageDao;
import com.hlingsoft.cms.dao.OrderInfoDao;
import com.hlingsoft.cms.dao.TypeDao;
import com.hlingsoft.cms.entity.*;
import com.hlingsoft.cms.entity.vo.JsonVo;
import com.hlingsoft.cms.entity.vo.PageVo;
import com.hlingsoft.cms.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.servlet.http.HttpServletRequest;


/**
 * Created by selbylei on 2015/5/7.
 */

@Controller
@RequestMapping("/manage/order")
public class ManageOrderAction extends ManageBaseAction {



    /**
     * 商品列表
     *
     * @param pageNum
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/list.htm")
    public String index(
            @RequestParam(value = "p", defaultValue = "1") int pageNum,
            ModelMap modelMap) {
        PageVo<OrderInfo> pageVo = new PageVo<OrderInfo>(pageNum);
        pageVo.setRows(10);
        pageVo.basePath = "/manage/order/list.htm";
        List<OrderInfo> orderInfoList = orderInfoDao.getOrderListFY(pageVo.getOffset(), pageVo.getRows());
        pageVo.setList(orderInfoList);
        pageVo.setCount(orderInfoDao.getOrderListCount());

        modelMap.put("p", pageNum);
        modelMap.put("pageVo", pageVo);

        return "manage/order/list";
    }


    /**
     * 添加界面
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/add.htm", method = RequestMethod.GET)
    public String add(
            ModelMap modelMap) {

        List<Type> typeList = typeDao.getTypeList();
        modelMap.put("typeList", typeList);
        return "manage/goods/add";
    }






    /**
     * 编辑界面
     * @param orderId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/check.htm")
    public String edit(
            @RequestParam(value = "orderId") long orderId,
            ModelMap modelMap) {
        OrderInfo order = orderInfoDao.getOrderById(orderId);
        modelMap.put("order",order);
        return "manage/order/check";
    }


    /**
     * 蛋糕审核
     * @param id
     * @param orderId
     * @param name
     * @param appUserId
     * @param goodsSize
     * @param goodsPrice
     * @param goodsCount
     * @param otherReq
     * @param payType
     * @param shippingType
     * @param request
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/check.json")
    @ResponseBody
    public JsonVo<String> update(
            @RequestParam(value = "id") long id,
            @RequestParam(value = "orderId") String orderId,
            @RequestParam(value = "name") String name,
            @RequestParam(value = "appUserId") Integer appUserId,
            @RequestParam(value = "goodsSize")String goodsSize,
            @RequestParam(value = "goodsPrice")Integer goodsPrice,
            @RequestParam(value = "goodsCount") int goodsCount,
            @RequestParam(value = "otherReq") String otherReq,
            @RequestParam(value = "payType")  int payType,
            @RequestParam(value = "shippingType")  int shippingType,
            @RequestParam(value = "shippingPrice") Integer shippingPrice,

            HttpServletRequest request, ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
            Date now = new Date();
            OrderInfo order=orderInfoDao.getOrderById(id);
            order.setOrderId(orderId);
            order.setName(name);
            order.setAppUserId(appUserId);
            order.setGoodsSize(goodsSize);
            order.setGoodsPrice(goodsPrice.doubleValue());
            order.setGoodsCount(goodsCount);
            order.setOtherReq(otherReq);
            order.setPayType(payType);
            order.setGoodsTotalPrice(goodsPrice.doubleValue() * goodsCount);
            order.setShippingType(shippingType);
            order.setShippingPrice(shippingPrice.doubleValue());
            order.setStatus(OrderInfoConstant.WAITPAY);
            order.setCreateTime(now);
            order.setUpdateTime(now);
            order.setPayPrice(order.getGoodsPrice()*order.getGoodsCount()-order.getCouponPrice()+order.getShippingPrice());

            orderInfoDao.updateOrderInfo(order);
            json.setResult(true);
            return json;
    }




    /**
     * 删除商品
     *
     * @param goodsId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonVo<String> delete(@RequestParam(value = "goodsId") long goodsId,
                                 ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        goodsDao.deleteGoodsById(goodsId);
        json.setResult(true);
        return json;
    }

}
